﻿Imports System.Data.SqlClient
Imports System.Configuration
Module Md_XdDS
    Public Ds As New DataSet("DsHD")
    Dim Dt As DataTable
    Dim DA As New SqlDataAdapter

    Sub TaoThemDtKhachHang()
        Dt = New DataTable("DtKhachHang")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_SelectKH"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub

    Sub TaoThemDtNhaCC()
        Dt = New DataTable("DtNhaCC")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_SelectNCC"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub

    Sub TaoThemDtHangHoa()
        Dt = New DataTable("DtHangHoa")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_SelectHangHoa"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub

    Sub TaoThemDtChiTietPhieuNhap()
        Dt = New DataTable("DtChiTietPhieuNhap")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_ChiTietPhieuNhap"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub

    Sub TaoThemDtChiTietHoaDon()
        Dt = New DataTable("DtChiTietHoaDon")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_ChiTietHoaDon"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub


    Sub TaoThemDtHoaDon()
        Dt = New DataTable("DtHoaDon")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_HoaDon"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub

    Sub TaoThemDtLoaiHang()
        Dt = New DataTable("DtLoaiHang")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_LoaiHang"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub


    Sub TaoThemDtPhieuNhap()
        Dt = New DataTable("DtPhieuNhap")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_PhieuNhap"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub


    Sub TaoThemDtTonKho()
        Dt = New DataTable("DtTonKho")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_TonKho"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub


    Sub TaoThemDtThangNam()
        Dt = New DataTable("DtThangNam")
        If Not Ds.Tables.Contains(Dt.TableName) Then
            Ds.Tables.Add(Dt)
        End If
        Md_KetNoi.XdKetNoi()

        DA.SelectCommand = New SqlCommand

        DA.SelectCommand.Connection = KetNoi
        DA.SelectCommand.CommandText = "Spr_ThangNam"
        DA.SelectCommand.CommandType = CommandType.StoredProcedure

        DA.Fill(Dt)
        Md_KetNoi.NgatKetNoi()
    End Sub



    Sub HienThiKQ()
        Dim SoTable As Integer = Ds.Tables.Count
        Dim DsBang As String = ""
        For i As Integer = 0 To SoTable - 1
            If i < (SoTable - 1) Then
                DsBang = DsBang & (i + 1) & "." & Ds.Tables(i).TableName & "(" & _
                Ds.Tables(i).Rows.Count & ")" & "," & vbNewLine
            Else
                DsBang = DsBang & (i + 1) & "." & Ds.Tables(i).TableName & "(" & _
                Ds.Tables(i).Rows.Count & ")"
            End If
        Next

        MsgBox(String.Format("Có {0} datatable trong dataset {1} g?m : {2}{3}", _
        SoTable, Ds.DataSetName, vbNewLine, DsBang))

    End Sub

    Sub QHKhachHang_HoaDon()
        If Not Ds.Relations.Contains("QHKH_HD") Then
            Dim DtKH As DataTable = Ds.Tables("DtKhachHang")
            Dim DtHD As DataTable = Ds.Tables("DtHoaDon")
            Dim QH01 As New DataRelation("QHKH_HD", DtKH.Columns("MAKH"), DtHD.Columns("MAKH"))
            Ds.Relations.Add(QH01)
        End If
    End Sub

    Sub QHThangNam_TonKho()
        If Not Ds.Relations.Contains("QHTN_TK") Then
            Dim DtTN As DataTable = Ds.Tables("DtThangNam")
            Dim DtTK As DataTable = Ds.Tables("DtTonKho")
            Dim QH02 As New DataRelation("QHTN_TK", DtTN.Columns("THANG"), DtTK.Columns("THANG"))
            Ds.Relations.Add(QH02)
        End If
    End Sub

    Sub QHTNhaCC_PhieuNhap()
        If Not Ds.Relations.Contains("QHNhaCC_PN") Then
            Dim DtNhaCC As DataTable = Ds.Tables("DtNhaCC")
            Dim DtPN As DataTable = Ds.Tables("DtPhieuNhap")
            Dim QH03 As New DataRelation("QHNhaCC_PN", DtNhaCC.Columns("MACC"), DtPN.Columns("MACC"))
            Ds.Relations.Add(QH03)
        End If
    End Sub

    Sub QHHoaDon_ChiTietHoaDon()
        If Not Ds.Relations.Contains("QHHD_CTHD") Then
            Dim DtHD As DataTable = Ds.Tables("DtHoaDon")
            Dim DtChiTietHD As DataTable = Ds.Tables("DtChiTietHoaDon")
            Dim QH04 As New DataRelation("QHHD_CTHD", DtHD.Columns("SOHD"), DtChiTietHD.Columns("SOHD"))
            Ds.Relations.Add(QH04)
        End If
    End Sub

    Sub QHHangHoa_ChiTietHoaDon()
        If Not Ds.Relations.Contains("QHHH_CTHD") Then
            Dim DtHH As DataTable = Ds.Tables("DtHangHoa")
            Dim DtChiTietHD As DataTable = Ds.Tables("DtChiTietHoaDon")
            Dim QH05 As New DataRelation("QHHH_CTHD", DtHH.Columns("SOHD"), DtChiTietHD.Columns("SOHD"))
            Ds.Relations.Add(QH05)
        End If
    End Sub

    Sub QHHangHoa_ChiTietPhieuNhap()
        If Not Ds.Relations.Contains("QHHH_CTPN") Then
            Dim DtHH As DataTable = Ds.Tables("DtHangHoa")
            Dim DtChiTietPN As DataTable = Ds.Tables("DtChiTietPhieuNhap")
            Dim QH06 As New DataRelation("QHHH_CTPN", DtHH.Columns("MAHANG"), DtChiTietPN.Columns("MAHANG"))
            Ds.Relations.Add(QH06)
        End If
    End Sub

    Sub QHLoaiHang_HangHoa()
        If Not Ds.Relations.Contains("QHLH_HH") Then
            Dim DtLH As DataTable = Ds.Tables("DtLoaiHang")
            Dim DtHH As DataTable = Ds.Tables("DtHangHoa")
            Dim QH07 As New DataRelation("QHLH_HH", DtLH.Columns("MALOAI"), DtHH.Columns("MALOAI"))
            Ds.Relations.Add(QH07)
        End If
    End Sub

    Sub QHPhieuNhap_ChiTietPhieuNhap()
        If Not Ds.Relations.Contains("QHPN_CTPN") Then
            Dim DtPN As DataTable = Ds.Tables("DtPhieuNhap")
            Dim DtChiTietPN As DataTable = Ds.Tables("DtChiTietPhieuNhap")
            Dim QH08 As New DataRelation("QHPN_CTPN", DtPN.Columns("SOPN"), DtChiTietPN.Columns("SOPN"))
            Ds.Relations.Add(QH08)
        End If
    End Sub

    Sub QHHangHoa_TonKho()
        If Not Ds.Relations.Contains("QHHH_TK") Then
            Dim DtHH As DataTable = Ds.Tables("DtHangHoa")
            Dim DtTK As DataTable = Ds.Tables("DtTonKho")
            Dim QH09 As New DataRelation("QHHH_TK", DtHH.Columns("MAHANG"), DtTK.Columns("MAHANG"))
            Ds.Relations.Add(QH09)
        End If
    End Sub









End Module
